package com.wnxy.admin.controller;

import com.github.pagehelper.PageInfo;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.common.handler.GlobalVariable;
import com.wnxy.admin.common.util.AliOSSUtils;
import com.wnxy.admin.dto.CustomerDto;
import com.wnxy.admin.dto.RegisterDto;
import com.wnxy.admin.service.ICarService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.wnxy.admin.entity.Customer;
import com.wnxy.admin.service.ICustomerService;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * guo
 * 客户表 前端控制器
 * </p>
 * 123456
 *
 * @author All
 * @since 2023-11-29
 */

@RestController
@RequestMapping("/customer")
public class CustomerController {

    private final ICustomerService customerServiceImpl;
    private final ICarService carServiceImpl;
    private final RedisTemplate redisTemplate;

    public CustomerController(ICustomerService customerServiceImpl, ICarService carServiceImpl, RedisTemplate redisTemplate) {
        this.customerServiceImpl = customerServiceImpl;
        this.carServiceImpl = carServiceImpl;
        this.redisTemplate = redisTemplate;
    }

    //分页，模糊查询
    @GetMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult page(@PathVariable Integer pageNum,
                               @PathVariable Integer pageSize,
                               @RequestParam(required = false) String keyword) {
        PageInfo<CustomerDto> page = customerServiceImpl.getAll(pageNum, pageSize, keyword);
        return ResponseResult.ok(page);
    }

    //新增
    @PostMapping("/register")
    public ResponseResult register(@RequestBody RegisterDto registerDto) {
        customerServiceImpl.register(registerDto);
        return ResponseResult.ok();
    }

    //禁用
    @PostMapping("/disable/{id}")
    public ResponseResult disable(@PathVariable("id") Integer id) {
        Customer customer = customerServiceImpl.getById(id);
        customer.setStatus(1);
        customerServiceImpl.updateById(customer);
        return ResponseResult.ok();
    }

    //编辑
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Customer customer) {
        customerServiceImpl.updateById(customer);
        redisTemplate.opsForSet().add("upload-file-db", customer.getImage());
        return ResponseResult.ok();
    }

    //根据id获取客户
    @GetMapping("/getById")
    public ResponseResult getById(Integer customerId) {
        Customer customer = customerServiceImpl.getById(customerId);
        return ResponseResult.ok(customer);
    }

    //获取全部
    @GetMapping("/getAll")
    public ResponseResult getAll() {
        List<Customer> list = customerServiceImpl.list();
        return ResponseResult.ok(list);
    }

    @GetMapping("/getCountById/{customerId}")
    public ResponseResult getCountById(@PathVariable Integer customerId) {
        Integer count = carServiceImpl.getCountById(customerId);
        return ResponseResult.ok(count);
    }

    @GetMapping("/getCarsById/{customerId}")
    public ResponseResult getCarsById(@PathVariable Integer customerId) {
        return ResponseResult.ok(carServiceImpl.getCarByCustomerId(customerId));
    }

}
